package org.obs.service.impl;

import org.acegisecurity.userdetails.UserDetails;
import org.obs.core.HibernateExecutor;
import org.obs.core.hibernate.operations.GetUserByEmailOperation;
import org.obs.core.hibernate.operations.GetUserIdByEmailOperation;
import org.obs.core.hibernate.operations.GetUserIdByNickOperation;
import org.obs.domain.User;
import org.obs.service.DetailsService;
import org.obs.service.UserService;
import org.springframework.transaction.annotation.Transactional;

public class UserServiceImpl implements UserService, DetailsService {

	private HibernateExecutor executor;

	protected UserServiceImpl() {

	}

	public UserServiceImpl(HibernateExecutor executor) {
		this.executor = executor;
	}

	protected User getByEmail(String email) {
		User user = executor.execute(new GetUserByEmailOperation(email));
		return user;
	}
	
	@Transactional
	public UserDetails getByUsername(String username) {
		return getByEmail(username).getUserDeatails();
	}

	@Transactional(readOnly=true)
	public Boolean isEmailUnique(String email) {
		return executor.execute(new GetUserIdByEmailOperation(email)) == null;
	}

	@Transactional(readOnly=true)
	public Boolean isNickUnique(String nick) {
		return executor.execute(new GetUserIdByNickOperation(nick)) == null;
	}

}
